package br.com.gerador.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ScrollPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import br.com.gerador.component.GComponent;
import br.com.gerador.component.GPage;
import br.com.gerador.icomponent.IGComponent;

public class Principal extends JFrame {
	public JPanel content;
	private Class btnSelecionado = null;
	private JPanel objSelect = null;
	private GComponent compCopyBuffer;
	private PnlPropriedades propriedades; 
	
	private GPage page;
	
	public Principal() {
		super("Gerador");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setExtendedState(JFrame.MAXIMIZED_BOTH);

		JMenuBar mb = new JMenuBar();
		mb.setOpaque(true);
		mb.setBackground(Color.LIGHT_GRAY);
		JMenu menu = new JMenu("Arquivo");
		JMenuItem item = new JMenuItem("Sair");
		menu.add(item);
		mb.add(menu);
		setJMenuBar(mb);

		JPanel panel = new JPanel(new BorderLayout());
		panel.add(new PnlInspect(this),BorderLayout.NORTH);
		propriedades = new PnlPropriedades();
		panel.add(propriedades,BorderLayout.LINE_START);

		content = new JPanel(null);
		//content.setSize(Short.MAX_VALUE, Short.MAX_VALUE);
		//content.setPreferredSize(new Dimension(600, 800));
		
		ScrollPane scroll = new ScrollPane();
		scroll.add(content);
		panel.add(scroll,BorderLayout.CENTER);

		setContentPane(panel);
		createPage();
		setVisible(true);
	}

	public void setBtnSelected(Class b){
		btnSelecionado = b;
	}

	private void createPage(){
		page = new GPage();
		page.setPrincipal(this);
		page.setBounds(20, 20, 0, 0);
		page.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
		page.setBackground(Color.WHITE);
		page.addMouseListener(mousePage);
		propriedades.setComponent(page);
		propriedades.bind();
		page.bindPropriedades();
		content.add(page);
		repaint();
	}
	
	public PnlPropriedades getPropriedades() {
		return propriedades;
	}
	
	public IGComponent createComponentInPage(int x, int y){
		try {
			if( btnSelecionado != null ){
				GComponent comp = (GComponent)btnSelecionado.newInstance();
				if( comp != null ){
					comp.initComponent(x, y);
					comp.setPage(page);
					propriedades.setComponent(comp);
					propriedades.bind();
					comp.bindPropriedades();
					page.add(comp,0);
					page.repaint();
					page.revalidate();
				}
				btnSelecionado = null;
				return comp;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void copyComponentInPage(GComponent antigo){
		try {
			compCopyBuffer = antigo.getClass().newInstance();
			compCopyBuffer.setPropriedades(antigo.getPropriedades());
			compCopyBuffer.bindPropriedades();
			compCopyBuffer.setBounds(antigo.getX() + 20, antigo.getY()+20, antigo.getWidth(), antigo.getHeight());
			compCopyBuffer.setPage(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void pasteComponentInPage(){
		try {
			if( compCopyBuffer != null ){
				propriedades.setComponent(compCopyBuffer);
				propriedades.bind();
				page.add(compCopyBuffer,0);
				page.repaint();
				page.revalidate();
				copyComponentInPage(compCopyBuffer);			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	MouseAdapter mousePage = new MouseAdapter(){
		@Override
		public void mouseReleased(MouseEvent e) {
			createComponentInPage(e.getX(),e.getY());
		}
	};		
	
}

